﻿using System;

namespace Silver3D.Graphics
{
    public class VertexInfo
    {
        private int posOffset, posSize;
        private bool hasNormal, hasTexCoord, hasColor;
        private int normalOffset, normalSize;
        private int texCoordOffset, texCoordSize;
        private int colorOffset, colorSize;
        private int size;

        public int PositionOffset { get { return posOffset; } }
        public int PositionSize { get { return posSize; } }

        public bool HasNormal { get { return hasNormal; } }
        public int NormalOffset { get { return normalOffset; } }
        public int NormalSize { get { return normalSize; } }

        public bool HasTexCoord { get { return hasTexCoord; } }
        public int TexCoordOffset { get { return texCoordOffset; } }
        public int TexCoordSize { get { return texCoordSize; } }

        public bool HasColor { get { return hasColor; } }
        public int ColorOffset { get { return colorOffset; } }
        public int ColorSize { get { return colorSize; } }

        public int Size { get { return size; } }

        public VertexInfo()
            : this(3, 0, 0, 0)
        {
        }

        public VertexInfo(int posSize)
            : this(posSize, 0, 0, 0)
        {
        }

        public VertexInfo(int posSize, int normalSize)
            : this(posSize, normalSize, 0, 0)
        {
        }

        public VertexInfo(int posSize, int normalSize, int texCoordSize)
            : this(posSize, normalSize, texCoordSize, 0)
        {
        }

        public VertexInfo(int posSize, int normalSize, int texCoordSize, int colorSize)
        {
            if (posSize != 3 && posSize != 4)
                throw new ArgumentException("posSize needs to be 3 or 4");

            if (normalSize != 3 && normalSize != 0)
                throw new ArgumentException("normalSize needs to be 3 or 0");

            if ((texCoordSize < 2 || texCoordSize > 4) && texCoordSize != 0)
                throw new ArgumentException("texCoordSize needs to be 2,3,4 or 0");

            if (colorSize != 3 && colorSize != 4 && colorSize != 0)
                throw new ArgumentException("colorSize needs to be 3,4 or 0");

            posOffset = 0;
            this.posSize = posSize;

            hasNormal = normalSize > 0;
            normalOffset = posOffset + posSize;
            this.normalSize = normalSize;

            hasTexCoord = texCoordSize > 0;
            texCoordOffset = normalOffset + normalSize;
            this.texCoordSize = texCoordSize;

            hasColor = colorSize > 0;
            colorOffset = texCoordOffset + texCoordSize;
            this.colorSize = colorSize;

            size = posSize + normalSize + texCoordSize + colorSize;
        }
    }
}
